import ajax from './ajax';
import { computed, ref } from 'vue';
import { getUsersByName, getSalesConsultants } from './dashboard/users';

export async function getDict(appId: number, code: string) {
  let api = `/Panels/Applications/${appId}/Dictionaries/Group/${code}`;
  let data = await ajax.get<Array<DictResult>>(api);
  return data
    .filter((a) => a.parentId > 0)
    .map((a) => {
      return {
        label: a.dictionaryName,
        value: a.dictionaryId,
      };
    }) as SelectOptionData[];
}

export function getOptions(code: string) {
  let opts = ref([] as SelectOptionData[]);
  setTimeout(async () => {
    let data = (await getDict(10, code)) as SelectOptionData[];
    opts.value = data;
  }, 1);
  return opts;
}


export function getAppOptions(code: string) {
  let opts = ref([] as SelectOptionData[]);
  setTimeout(async () => {
    let data = (await getDict(110, code)) as SelectOptionData[];
    opts.value = data;
  }, 1);
  return opts;
}

export async function getDictName(id: number) {
  let api = `/ControlPanels/Dictionaries/${id}`;
  let data = await ajax.get<DictResult>(api);
  return data.dictionaryName;
}

export async function getDictRemark(id: number) {
  let api = `/ControlPanels/Dictionaries/${id}`;
  let data = await ajax.get<DictResult>(api);
  return data.remark;
}

export function getUserOptions(name: string) {
  let opts = ref([] as SelectOptionData[]);
  setTimeout(async () => {
    let data = (await getUserIdAndName(name)) as SelectOptionData[];
    opts.value = data;
  }, 1);
  return opts;
}

export async function getUserIdAndName(name: string) {
  let data = await getUsersByName(name);
  return data
    .filter((a) => a.enabled)
    .map((a) => {
      return {
        label: a.userName,
        value: a.userId,
      };
    }) as SelectOptionData[];
}

